package com.lin;

import java.util.HashMap;
import java.util.Map;

public class Solution290 {
    public static void main(String[] args) {
        wordPattern("abba","dog dog dog dog");
    }
//  "abba"
//  "dog dog dog dog"
    public static  boolean wordPattern(String pattern, String s) {
        char[] chars = pattern.toCharArray();
        String[] strings =  s.split(" ");
        if (chars.length != strings.length)
            return false;
        Map<Character,String> map_keyTovalue = new HashMap<>();
        Map<String, Character> map_valueTokey = new HashMap<>();
        for (int i=0, j=0;i<chars.length&&j<strings.length;i++,j++){
            if (!map_keyTovalue.containsKey(chars[i])&&!map_valueTokey.containsKey(strings[j])){
                map_keyTovalue.put(chars[i],strings[j]);
                map_valueTokey.put(strings[j],chars[i]);
            }
            else {
                if (map_keyTovalue.containsKey(chars[i])){
                    if (!map_keyTovalue.get(chars[i]).equals(strings[j]))
                        return false;
                }
                if (map_valueTokey.containsKey(strings[j])){
                    if (!map_valueTokey.get(strings[j]).equals(chars[i])){
                        return false;
                    }
                }
            }
        }
        return true;
    }
}
